﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using Model.Query_Model;
using Model.Filters;
using Datasource;

namespace Presenters.Managers
{
    public static class ValuationManager
    {
        private static IList<ValuationOutput> valuationList = new List<ValuationOutput>();

        private static IList<ValuationOutput> BuildCollection(DbDataReader reader)
        {
            valuationList.Clear();
            while (reader.Read())
            {
                valuationList.Add(new ValuationOutput
                {
                    idValuation=reader.GetInt32(0),
                    value=reader.GetDouble(1),
                    isActually = reader.GetBoolean(2),
                    dateModify=reader.GetDateTime(3),
                    currency=(CurrencyOutput)CurrencyManager.CurrencySearch(new CurrencyCriteria{ idValuation=reader.GetInt32(0)}).Take(1).FirstOrDefault<CurrencyOutput>()
                }
                );
            }
            return valuationList;
        }

        public static IList<ValuationOutput> ValuationSearch(ValuationCriteria criteria)
        {
            DbConnection connection = DbUtil.GetActivityConnection();
            DbCommand command = new SqlCommand();
            DbUtil.SetConnection(connection, command);
            DbUtil.SetQuery(command, new DbQuery { sql = "exec dbo.[ValuationSearch] @idRoad" });
            DbUtil.SetParametr(command, "@idRoad", criteria.idRoad);
            return BuildCollection(command.ExecuteReader());
        }
    }
}
